<link rel="stylesheet" href="../../node_modules/bootstrap/dist/css/bootstrap.min.css">
<link rel="stylesheet" href="./../../node_modules/bootstrap-icons/font/bootstrap-icons.css">
<link rel="stylesheet" href="../index.css">
<div class="container-fluid" id="app">
    <div class="row pt-2">
        <div class="col"><textarea class="form-control form-control-sm" style="resize:none;" v-model="strUrl" rows="7" placeholder="请输入url"></textarea></div>
    </div>
    <div class="row mt-3">
        <div class="col text-center">
            <button type="button" class="btn btn-secondary btn-sm" @click="clear">全部清空</button>&nbsp;&nbsp;
            <button type="button" class="btn btn-primary btn-sm" @click="encode"><i class="bi bi-arrow-down"></i>&nbsp;URL编码</button>&nbsp;&nbsp;
            <button type="button" class="btn btn-primary btn-sm" @click="decode"><i class="bi bi-arrow-up"></i>&nbsp;URL解码</button>
        </div>
    </div>
    <div class="row mt-2">
        <div class="col">
            <label>encodeURI</label>
        </div>
    </div>
    <div class="row">
        <div class="col"><textarea class="form-control form-control-sm" style="resize:none;" v-model="strEncode" rows="7"></textarea></div>
    </div>
    <div class="row mt-2">
        <div class="col">
            <label>encodeURIComponent</label>
        </div>
    </div>
    <div class="row">
        <div class="col"><textarea class="form-control form-control-sm" style="resize:none;" v-model="strEncodeComponent" rows="7"></textarea></div>
    </div>
</div>
<script src="../../node_modules/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
<script src="../../node_modules/vue/dist/vue.global.prod.js"></script>
<script>
const app = {
    data() {
        return {
            strUrl: '',
            strEncode: '',
            strEncodeComponent: ''
        }
    },
    created() {
        document.addEventListener('keyup', (e) => {
            if (e.ctrlKey && e.shiftKey && (e.key == 'I' || e.key ==  'i')) {
                parent.devTools();
            }
            if (e.ctrlKey && (e.key == 'r' || e.key == 'R')) {
                parent.refresh();
            }
        });
    },
    methods: {
        encode() {
            // try {
            //     new URL(this.strUrl)
            // } catch (error) {
            //     console.error(error);
            //     alert('不正确的URL')
            //     return;
            // }
            this.strEncode = encodeURI(this.strUrl);
            this.strEncodeComponent = encodeURIComponent(this.strUrl);
        },
        decode() {
            if(this.strEncode != '') {
                this.strUrl = decodeURI(this.strEncode)
                this.strEncodeComponent = encodeURIComponent(this.strUrl);
            } else if(this.strEncodeComponent != '') {
                this.strUrl = decodeURIComponent(this.strEncodeComponent);
                this.strEncode = encodeURI(this.strUrl);
            } else {
                this.strUrl = '';
            }
        },
        clear() {
            this.strUrl = '';
            this.strEncode = '';
            this.strEncodeComponent = '';
        }
    }
}
Vue.createApp(app).mount('#app');
</script>